<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>营销商品</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport"
			content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
		<style type="text/css">
			td .layui-table-cell {
				text-align: center;
				height: 100px;
				white-space: normal;
			}

			.layui-table img,
			.layui-table video {
				width: 50px;
				max-width: 60px;
			}

			td .layui-table-cell {
				text- align: center;
				height: auto;
				white-space: normal;
			}

			.tDiv {
				font-size: 10px;
				line-height: 13px;
			}

			.tDiv-price {
				font-size: 10px;
				line-height: 16px;
			}
		</style>
	</head>

	<body>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-header demoTable">
						<!-- 	<button class="layui-btn layui-btn-normal layui-btn-sm" data-type='alladd'>添加</button>
							<button class="layui-btn layui-btn-warm layui-btn-sm" data-type='alledit'>编辑</button> -->
							<button class="layui-btn layui-btn-danger layui-btn-sm" id="del">删除</button>
						</div>
						<div class="layui-card-body">

							<div class="layui-row layui-col-space15">
								<div class="layui-btn-sm2 layui-col-md2 layui-col-xs2">
									<div id="test9" class="demo-tree demo-tree-box" style="width: 100%;"></div>
								</div>
								<div class="layui-btn-sm10 layui-col-md10 layui-col-xs10">
									<!-- 搜索 -->
									<form class="layui-form search" id="form" onsubmit="return false" action="##">

										<div class="layui-inline search_inline">
											<!-- <label class="layui-form-label">当前活动: <span id="activeName"></span></label> -->
											<label class="layui-form-label">商品编号：</label>
											<div class="layui-input-inline">
												<input class="layui-input" name="skuid" placeholder="商品编号"
													autocomplete="off">
											</div>
										</div>
										<div class="layui-inline search_inline">
											<label class="layui-form-label">商品名称：</label>
											<div class="layui-input-inline">
												<input class="layui-input" name="skuname" placeholder="商品名称"
													autocomplete="off">
											</div>
										</div>

										<button class="layui-btn search_inline" lay-submit="" lay-filter="search"
											data-type="reload">搜索</button>
										<button class="layui-btn layui-btn-primary search_inline"
											type="reset">重置</button>
									</form>
									<!-- 搜索 -->
									<table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate">
									</table>
								</div>
							</div>

							<table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>

							<script type="text/html" id="zizeng">
							{{d.LAY_TABLE_INDEX+1}}
							</script>
							<script type="text/html" id="goods_info">
								<div class="tDiv relative tpinfo last " style="text-align: left;">
									<div class="img" style="display: inline-block;vertical-align: top;">
										<img width="50" src={{d.goods_image_url}} alt="">
									</div>
									<div class="name mb5" style="display: inline-block;vertical-align: top;">
										商品编号:{{d.skuid?d.skuid:''}}
										<br /> 商品名称:{{d.skuname?d.skuname:''}}
										<br /> 规格参数:{{d.specparam?d.specparam:''}}
									</div>
								</div>
							</script>
							<script type="text/html" id="goods_price">
								<div class="tDiv-price" style="text-align: left;">
									市场价：
									<span class="line-text"> {{d.price?d.price:''}}</span>
									<br /> 会员价：
									<span class="line-text"> {{d.price_member?d.price_member:''}}</span>
									<br /> 拼团价：
									<span class="line-text"> {{d.seckill_price?d.seckill_price:''}}</span>
									<br /> 补贴：
									<span class="line-text"> {{d.subsidy?d.subsidy:''}}</span>
								</div>
							</script>
							<script type="text/html" id="limited_count">
								<div class="tDiv-price" style="text-align: left;">
									活动总数：
									<span class="line-text"> {{d.stock?d.stock:0}}</span>
									<br /> 已购数量：
									<span class="line-text"> {{d.sales?d.sales:0}}</span>
									<br /> 剩余数量：
									<span class="line-text"> {{(d.stock-d.sales)?d.stock-d.sales:0}}</span>
									<br />
									显示数量:
									<span class="line-text"> {{(d.num)?d.num:0}}</span>
									<br />
								</div>
							</script>
							<script type="text/html" id="seckill_group">
								<div class="tDiv-price" style="text-align: left;">
									团人数：
									<span class="line-text"> {{d.group_num?d.group_num:0}}</span>
									<br /> 中奖人数：
									<span class="line-text"> {{d.group_wnum?d.group_wnum:0}}</span>
									<br />
								</div>
							</script>
							<script type="text/html" id="seckill_fee">
								<div class="tDiv-price" style="text-align: left;">
									团长佣金：
									<span class="line-text"> {{d.leader_fee?d.leader_fee:0}}</span>
									<br /> 直推佣金：
									<span class="line-text"> {{d.direct_fee?d.direct_fee:0}}</span>
									<br /> 间推佣金：
									<span class="line-text"> {{d.indirect_fee?d.indirect_fee:0}}</span>
									<br /> 管理奖：
									<span class="line-text"> {{d.team_fee?d.team_fee:0}}</span>
									<br /> 合伙人佣金：
									<span class="line-text"> {{d.copartner_fee?d.copartner_fee:0}}</span>
									<br /> 服务站佣金：
									<span class="line-text"> {{d.station_fee?d.station_fee:0}}</span>
									<br />
								</div>
							</script>
							<script type="text/html" id="time_line">
								<div class="tDiv-price" style="text-align: left;">
									开始时间：
									<span class="line-text"> {{d.times?d.dates+' '+d.times:''}}</span>
									<br /> 结束时间：
									<span class="line-text"> {{d.endtime?d.endtime:''}}</span>
								</div>
							</script>

							<script type="text/html" id="test-table-operate-barDemo">
								<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
							</script>
						</div>
					</div>
				</div>
			</div>
		</div>

		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/common.js"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'table', 'form', 'laydate', 'tree'], function() {
				var table = layui.table,
					$ = layui.$,
					laydate = layui.laydate,
					form = layui.form,
					tree = layui.tree,
					admin = layui.admin;

				var _data = {
					seckill_id: '',
					name: ''
				};

				// initdata();
				initdata2()
				function initdata2() {
					$.post(server.queryshopmodulelist, function(res, status) {
						if(status === 'success') {
							if(res.code === 0){
								let list = fnSetTreeData1(res.module_list.data)
								list.forEach(item => {
									item.spread = true
								})
								// console.log(list)
								initdata(list[1].id)
								tree.render({
									elem: '#test9',
									data: list,
									click: function(obj) {
										initdata(obj.data.id)
									}
								})
							}
						}
					})
				}
				
				 //通过转换 达到符合tree的数据要求  
				 function fnSetTreeData1(data) {
					var data = [...data];
					var tree = data.filter((father) => {
					var branchArr = data.filter((child) => {
							if (father.id == child.sid.split(',')[1]) child._hasParent = true; return father.id == child.sid.split(',')[1]; });
				                      if (branchArr.length > 0) father.children = branchArr; 
				                      fnSetTreeData2(branchArr)
				                      return !father._hasParent;
				                    });  
				                    tree = tree.filter((item) => {
				                      return !item._hasParent;
				                    })
				                    return tree
				                  } 
				                  function fnSetTreeData2(data) {
				                    var data = [...data];
				                    var tree = data.filter((father) => {
				                      var branchArr = data.filter((child) => {
				                        if (father.id == child.sid.split(',')[2]) child._hasParent = true; 
				                        return father.id == child.sid.split(',')[2]; 
				                      });
				                      if (branchArr.length > 0) father.children = branchArr;  
				                      return !father._hasParent;
				                    });  
				                    tree = tree.filter((item) => {
				                      return !item._hasParent;
				                    })
				                    return tree
				 }
				
				function initdata(id) {
					table.render({
						elem: '#test-table-operate',
						// url:server.shop_promotion_insert  
						url: server.shop_promotion_query + '&channelid=' + id,
						height: 'full-150',
						cellMinWidth: cellMinWidth,
						limit: limit,
						limits: limits,
						cols: [
							[{
								width: 50,
								field: 'zizeng',
								title: '#',
								templet: '#zizeng'
							}, {
								type: 'checkbox',
							}, {
								align: 'center',
								toolbar: '#test-table-operate-barDemo',
								title: '操作',
							}, 
							{
								field: 'status',
								title: '状态',
								width: 100,
								templet: function(d) {
									var html = ''
									if (d.status == 1) {
										html = '<input type="checkbox" name="status" value=' + d.id +
											' lay-skin="switch" lay-text="启用|停用" lay-filter="status" checked>'
									} else {
										html = '<input type="checkbox" name="status" value=' + d.id +
											' lay-skin="switch" lay-text="启用|停用" lay-filter="status">'
									}
									return html;
								}
							},
							{
								field: 'client', 
								title: '客户端',
								width: 180,
								templet: function(d) {
									var type = d.client;
									if(type) {
										var client_leng = type.split(',');
										var client_show = ''
										for(var i = 0; i < client_leng.length; i++) {
											client_show += icon_type_show(1, client_leng[i]) + ','
										}
										return client_show
									} else {
										return ''
									}
								}
							},
							{
								field: 'title', 
								width: 250,
								title: '标题(后台分区)'
							},
							{
								field: 'name',
								width: 250,
								title: '名称(客户端分区)'
							},
							{
								field: 'type',
								title: '活动类型',
								width: 120,
								templet: function(d) {
									return String(d.type) ? seckill_type_show(1,d.type):'';
								}
							},
							{
								field: 'top_count',
								title: '购买上限'
							},
							{
								field: 'dates',
								width: 180,
								title: '开始日期',
								sort: true
							},
							{
								field: 'icon_url',
								width: 90,
								title: '图标',
								templet: function(d) {
									return d.icon_url ? '<a class="text_blue_color" href="javascript:;" lay-event="seeicon">查看</a>' : ''
								}
							},
							{
								field: 'img_url',
								title: '主图',
								width: 90,
								templet: function(d) {
									return d.img_url ? '<a class="text_blue_color" href="javascript:;" lay-event="seeimg">查看</a>' : ''
								}
							},
							{
								field: 'istimer',
								width: 100,
								title: '是否倒计时',
								templet: function(d) {
									if(d.istimer == 1) {
										return '<span class="text_green_color">' + '是' + '</span>'
									} else if(d.istimer == 0) {
										return '<span class="text_red_color">' + '否' + '</span>'
									} else {
										return ''
									}
								}
							},
							{
								field: 'times',
								width: 180,
								title: '开始时间',
								sort: true
							},
							{
								field: 'endtime',
								width: 180,
								title: '结束时间',
								sort: true
							},
							{
								field: 'notes',
								width: 100,
								title: '备注',
							},
							{
								field: 'add_by',
								width: 100,
								title: '创建人',
							},
							{
								field: 'addtime',
								width: 160,
								title: '创建时间',
							},
							{
								field: 'edit_by',
								width: 100,
								title: '更新人',
							},
							{
								field: 'edittime',
								width: 160,
								title: '更新时间',
							}
							]
						],
						page: true,
						where: _data,
						request: {
							pageName: 'page',
							limitName: 'size' //每页数据量的参数名，默认：limit
						},
						parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
							// var date1 = res.seckill.data;
							// console.log(date1, 'date1')
							// _data.seckill_id = date1[0].id
							// _data.name = date1[0].title
							// let span = document.getElementById('activeName')
							// span.innerHTML = date1[0].title
							// tree.render({
							// 	elem: '#test9',
							// 	data: date1,
							// 	onlyIconControl: true,
							// 	click: function(obj) {
							// 		var data = obj.data
							// 		//data.id == 1 ? '' : data.id
							// 		_data.seckill_id = data.id;
							// 		_data.name = data.title;
							// 		initdata();  
							// 	}
							// });
							return {
								"code": res.code,
								"msg": res.msg,
								"count": res.seckill.pager.total,
								"data": res.seckill.data
							};
						},
						done: function() {
							//lay-tree的颜色 
							var nodes = document.getElementsByClassName("layui-tree-txt");
							for (var i = 0; i < nodes.length; i++) {
								if (nodes[i].innerHTML === _data.name)
									nodes[i].style.color = "red";
								else
									nodes[i].style.color = "#555";
							}
						},
					});
				}
				//监听搜索
				form.verify({
					memberid: function(value, item) { //value：表单的值、item：表单的DOM对象
						if (!value) {
							return '姓名不能为空';
						}
					}
				});

				//监听搜索
				form.on('submit(search)', function(data) {
					Object.assign(_data, data.field)
					initdata();
					return false;
				});

				$("button[type=reset]").on("click", function() {
					_data = {}
					initdata();
					document.getElementById("form").reset();
				});

				//监听表格复选框选择
				table.on('checkbox(test-table-operate)', function(obj) {
					console.log(obj)
				});

				//监听工具条
				table.on('tool(test-table-operate)', function(obj) {
					var data = obj.data;
					if (obj.event === 'edit') {
						console.log('修改')
						layer.open({
							type: 2,
							title: '修改',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['1200px', '90%'],
							content: 'model_s/collage_edit_add.html?id=' + data.id,
							end: function() {}
						});
					}
				});

				$('#del').on('click', function() {
					var checkStatus = table.checkStatus('test-table-operate'),
						data = JSON.stringify(checkStatus.data);
					var id = "";
					for (var i = 0; i < checkStatus.data.length; i++) {
						id += checkStatus.data[i].id;
						if (i != checkStatus.data.length - 1) {
							id += ",";
						}
					}
					layer.confirm('确定要删除所选的' + checkStatus.data.length + '条数据？', function() {
						$.ajax({
							url: server.seckill_goods_delete,
							type: 'post',
							dataType: 'json',
							data: "id=" + id,
							success: function(data) {
								if (data.code == '0') {
									layer.msg('删除成功');
									initdata();
								} else {
									layer.msg('删除失败');
									initdata();
								}
							},
							'error': function() {
								layer.msg('系统错误');
								initdata();
							}
						})
					});
				});

				var $ = layui.$,
					active = {
						alladd: function() {
							layer.open({
								type: 2,
								title: '添加',
								shadeClose: false,
								shade: 0.5,
								maxmin: true, //开启最大化最小化按钮
								area: ['100%', '100%'],
								content: 'model/promotion_list_add.html',
								success: function(layero, index) {

									var iframe = window['layui-layer-iframe' + index]
									iframe.child(_data, index)
								},
								end: function() {
									// initdata();
								}
							});
						},
						// alladd: function() {
						// 	var url = 'views/actives/shop_promotion/model/promotion_list_add.html'
						// 	if (top.layui.index) {
						// 		top.layui.index.openTabsPage(url, '添加店铺活动')
						// 	} else {
						// 		window.open(url)
						// 	}
						// 	// layer.open({
						// 	// 	type: 2,
						// 	// 	title: '添加',
						// 	// 	shadeClose: false,
						// 	// 	shade: 0.5,
						// 	// 	maxmin: true, //开启最大化最小化按钮
						// 	// 	area: ['60%', '90%'],
						// 	// 	content: 'model/edit_add.html',
						// 	// 	end: function() {
						// 	// 	}
						// 	// });
						// },
						alledit: function() {
							var checkStatus = table.checkStatus('test-table-operate'),
								data = JSON.parse(JSON.stringify(checkStatus.data))
							if (data.length !== 0) {
								layer.open({
									type: 2,
									title: '更新',
									shadeClose: false,
									shade: 0.5,
									maxmin: true,
									area: ['95%', '90%'],
									content: 'model_s/active_edit_add.html',
									success: function(layero, index) {
										var checkStatus = table.checkStatus('test-table-operate'),
											data = JSON.stringify(checkStatus.data)
										var id = "";
										for (var i = 0; i < checkStatus.data.length; i++) {
											id += checkStatus.data[i].id
											if (i != checkStatus.data.length - 1) {
												id += ',';
											}
										}
										var iframe = window['layui-layer-iframe' + index]
										iframe.child(id, index)
									},
									end: function() {

									}
								})
							} else {
								layer.msg('请先勾选商品')
							}
							// layer.open({
							// 	type: 2,
							// 	title: '更新',
							// 	shadeClose: false,
							// 	shade: 0.5,
							// 	maxmin: true, //开启最大化最小化按钮
							// 	area: ['95%', '90%'],
							// 	content: 'model_s/active_edit_add.html',
							// 	success: function(layero, index) {
							// 		var checkStatus = table.checkStatus('test-table-operate'),
							// 			data = JSON.stringify(checkStatus.data);
							// 		var id = "";
							// 		for(var i = 0; i < checkStatus.data.length; i++) {
							// 			id += checkStatus.data[i].skuid;
							// 			if(i != checkStatus.data.length - 1) {
							// 				id += ",";
							// 			}
							// 		} 
							// 		var iframe = window['layui-layer-iframe' + index]
							// 		iframe.child(id, index)
							// 	},
							// 	end: function() {
							// 		// initdata();
							// 	}
							// });
						},
						getCheckData: function() { //获取选中数据
							var checkStatus = table.checkStatus('test-table-operate'),
								data = checkStatus.data;
							layer.alert(JSON.stringify(data));
						},
						getCheckLength: function() { //获取选中数目
							var checkStatus = table.checkStatus('test-table-operate'),
								data = checkStatus.data;
							layer.msg('选中了：' + data.length + ' 个');
						},
						isAll: function() { //验证是否全选
							var checkStatus = table.checkStatus('test-table-operate');
							layer.msg(checkStatus.isAll ? '全选' : '未全选')
						}
					};

				$('.demoTable .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});

			});
		</script>
	</body>

</html>
